﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Support.V4.Widget;
using Android.Support.V7.App;
using Android.Support.V7.Widget;
using Android.Views;
using Android.Widget;
using mb;
using mb.Droid;
using Xamarin.Forms.Platform.Android;
using static Android.Support.V7.Widget.GridLayoutManager;

namespace libmp
{
    public class TestView : BaseMixList<DataStore.VideoItem>
    {
        public static MyFragment CreateFragment()
        {
            return new MyFragment()
            {
                Title = "测试",
                    MyOnCreateView = (f, container) =>
                    {
                        return new TestView(f).CreateView();
                    },
            };
        }

        MyFragment fragment;
        public TestView(MyFragment f)
        {
            this.fragment = f;
        }

        public View CreateView2()
        {
            LinearLayout layout = new LinearLayout(activity);
            layout.Orientation = Orientation.Vertical;
            layout.SetBackgroundColor(Color.Blue);

            dataSource = DataStore.store.GetAllVideo().Take(30).ToList();

            var colors = new Color[]
            {
                Color.Red,
                // Color.Black, Color.AliceBlue, Color.Pink, Color.Yellow,
                // Color.Violet,
                // Color.AntiqueWhite,
            };
            for (int i = 0; i < colors.Length; i++)
            {
                var height = 200 + 100 * i;
                var insertView = new TextView(activity);
                insertView.SetBackgroundColor(colors[i]);
                insertView.Text = "insert" + i;
                insertView.LayoutParameters = new ViewGroup.LayoutParams(-1, height);
                insertItems.Add(new InsertItem()
                {
                    view = insertView,
                        insertBefore = i * 4,
                        height = height,
                });
            }
            insertItems.Last().insertBefore = dataSource.Count;

            Setup();
            Reload();

            var lp = new LinearLayout.LayoutParams(-1, -1);
            lp.TopMargin = 200;
            layout.AddView(GetListView(), lp);

            return layout;
        }

        protected override View CreateCell(Dictionary<string, object> viewData)
        {
            // var view = new View(activity);
            var view = new MySquareLayout(activity, 1);
            view.SetBackgroundColor(Color.Green);

            return view;
        }

        public View CreateView()
        {
            LinearLayout layout = new LinearLayout(activity);
            layout.Orientation = Orientation.Vertical;
            // layout.SetBackgroundColor(Color.Blue);
            var fp = new FrameLayout(activity);
            fp.SetParent(layout);
            fp.LayoutParameters = new LinearLayout.LayoutParams(600, 600);
            fp.Id = View.GenerateViewId();

            logger.Log("load xforms....");
            // return layout;
            var page = new Shared.OfflineItemPage(null, null);
            var xfrag = page.CreateSupportFragment(this.fragment.Activity);
            this.fragment.ChildFragmentManager.BeginTransaction()
                .Replace(fp.Id, xfrag)
                .Commit();

            // page.Update(DataStore.store.GetVideoItemById(1));

            return layout;
        }
    };

}